package cn.stronglink.asset.module.resource.resourcegroup.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.ResourceGroup;
import cn.stronglink.asset.model.ResourceGroupAsset;
import cn.stronglink.asset.module.resource.resourcegroup.mapper.ResourceGroupMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceGroupService {
	
	@Autowired 
	private ResourceGroupMapper resourceGroupMapper;
	
	public void create(ResourceGroup resourceGroup) {
		if(resourceGroup.getId()==null) {
			resourceGroup.setId(IdWorker.getId());
		}
		resourceGroup.setCreateTime(new Date());
		resourceGroup.setCreateBy(WebUtil.getCurrentUser());
		resourceGroupMapper.create(resourceGroup);
		
		if(resourceGroup.getResourceGroupAssets()!=null&&resourceGroup.getResourceGroupAssets().size()>0) {
			for(ResourceGroupAsset resourceGroupAsset:resourceGroup.getResourceGroupAssets()) {
				resourceGroupAsset.setId(IdWorker.getId());
				resourceGroupAsset.setResourceGroupId(resourceGroup.getId());
				resourceGroupAsset.setCreateTime(new Date());
				resourceGroupAsset.setCreateBy(WebUtil.getCurrentUser());
				resourceGroupMapper.createResourceGroupAsset(resourceGroupAsset);
			}
		}
	}

	
	public void edit(ResourceGroup resourceGroup) {
		resourceGroup.setUpdateBy(WebUtil.getCurrentUser());
		resourceGroup.setUpdateTime(new Date());
		resourceGroupMapper.update(resourceGroup);
		resourceGroupMapper.removeResourceGroupAsset(resourceGroup.getId());
		if(resourceGroup.getResourceGroupAssets()!=null&&resourceGroup.getResourceGroupAssets().size()>0) {
			for(ResourceGroupAsset resourceGroupAsset:resourceGroup.getResourceGroupAssets()) {
				resourceGroupAsset.setId(IdWorker.getId());
				resourceGroupAsset.setResourceGroupId(resourceGroup.getId());
				resourceGroupAsset.setCreateTime(new Date());
				resourceGroupAsset.setCreateBy(WebUtil.getCurrentUser());
				resourceGroupMapper.createResourceGroupAsset(resourceGroupAsset);
			}
		}
	}

	public void remove(Long id) {
		resourceGroupMapper.removeResourceGroup(id);
		resourceGroupMapper.removeResourceGroupAsset(id);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<ResourceGroup> queryList(Page<ResourceGroup> page, Map<String, Object> params) {
		List<ResourceGroup> list = resourceGroupMapper.qryList(page,params);
		page.setRecords(list);
		return page;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public ResourceGroup qryById(Long id) {
		ResourceGroup resourceGroup = resourceGroupMapper.qryById(id);
		List<ResourceGroupAsset> assetlist = resourceGroupMapper.qryResourceGroupAssetList(id);
		resourceGroup.setResourceGroupAssets(assetlist);
		return resourceGroup;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<ResourceGroup> queryAllList() {
		List<ResourceGroup> list = resourceGroupMapper.queryAllList();
		return list;
	}
	
	

}
